import java.util.StringTokenizer;


class P567_39643 {

	public static void main(String[] args) {
		
		String line = readLn();
		
		int n = 1;
		
		while (line != null) {
			System.out.println("Test Set #" + n);
			process(line);
			n++;
			line = readLn();
		}
		
	}
	
	private static void process(String line) {
		int[][] board = new int[20][20];
		
		for (int k = 0; k < 20; k++)
			for (int j = 0; j < 20; j++) {
				board[k][j] = 99999999;
			}
		
		StringTokenizer st = new StringTokenizer(line);
		
		int numberOfBorders = Integer.parseInt(st.nextToken());
		
		int country;
		
		for (int k = 0; k < numberOfBorders; k++) {
			country = Integer.parseInt(st.nextToken());
			board[0][country-1] = 1;
			board[country-1][0] = 1;
		}
		
		for (int k = 1; k < 19; k++) {
		
			st = new StringTokenizer(readLn());
			
			numberOfBorders = Integer.parseInt(st.nextToken());
			
			
			for (int j = 0; j < numberOfBorders; j++) {
				country = Integer.parseInt(st.nextToken());
				board[k][country-1] = 1;
				board[country-1][k] = 1;
			}
			
		}

		floydWarshall(board);
		
		int numberOfTestCases = Integer.parseInt(new StringTokenizer(readLn()).nextToken());
		
		for (int k = 0; k < numberOfTestCases; k++) {
			st = new StringTokenizer(readLn());
			
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			
			if (x < 10) System.out.print(" ");
			System.out.print(x + " to ");
			if (y < 10) System.out.print(" ");
			System.out.print(y + ": ");
			System.out.println(board[x-1][y-1]);
		}
		
		System.out.println();
		
	}

	private static void floydWarshall(int[][] board) {
		for (int k = 0; k < 20; k++)
			board[k][k] = 0;
		
		
		for (int k = 0; k < 20; k++) {
			for (int i = 0; i < 20; i++) {
				for (int j = 0; j < 20; j++) {
					if (board[i][j] > board[i][k] + board[k][j]) {
						board[i][j] = board[i][k] + board[k][j];
					}
				}
			}
		}
		
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

}
